package com.javalong.androidmorning.dwz.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.javalong.androidmorning.constant.Constant;
import com.javalong.androidmorning.constant.StatusCode;
import com.javalong.androidmorning.dwz.vo.ResponseVO;
import com.javalong.androidmorning.pojo.Keyword;
import com.javalong.androidmorning.wechat.service.KeywordService;

@Controller
@RequestMapping("dwz/keyword")
public class KeywordController {

	@Autowired
	KeywordService keywordService;

	@RequestMapping("list")
	public String list(Model model) {
		List<Keyword> keywordList = keywordService.getKeywordList(0, Constant.PAGE_SIZE);
		model.addAttribute(keywordList);
		model.addAttribute("totalCount", keywordService.getCount());
		model.addAttribute("currentPage", 1);
		model.addAttribute("numPerPage", Constant.PAGE_SIZE);
		return "keyword/list";
	}

	@RequestMapping("listByPage")
	public String listByPage(@RequestParam("pageNum") int pageNum, @RequestParam("numPerPage") int numPerPage,
			Model model) {
		List<Keyword> keywordList = keywordService.getKeywordList(pageNum - 1, numPerPage);
		model.addAttribute(keywordList);
		model.addAttribute("totalCount", keywordService.getCount());
		model.addAttribute("currentPage", pageNum);
		model.addAttribute("numPerPage", numPerPage);
		return "keyword/list";
	}

	@RequestMapping("delete")
	@ResponseBody
	public ResponseVO delete(@RequestParam("id") int id) {
		keywordService.deleteKeyword(id);
		ResponseVO response = new ResponseVO();
		response.setStatusCode(StatusCode.SUCCESS);
		response.setMessage("删除成功!");
		response.setNavTabId("keyword_list");
		return response;
	}

	@RequestMapping("addjsp")
	public String addjsp() {
		return "keyword/add";
	}

	@RequestMapping("add")
	@ResponseBody
	public ResponseVO add(Keyword keyword) {
		keywordService.saveKeyword(keyword);
		ResponseVO response = new ResponseVO();
		response.setStatusCode(StatusCode.SUCCESS);
		response.setMessage("添加成功!");
		response.setNavTabId("keyword_list");
		response.setCallbackType("closeCurrent");
		return response;
	}

	@RequestMapping("updatejsp")
	public String updatejsp(@RequestParam("id")int id,Model model) {
		Keyword keyword = keywordService.findKeywordById(id);
		if(keyword!=null){
			model.addAttribute(keyword);
		}
		return "keyword/update";
	}

	@RequestMapping("update")
	@ResponseBody
	public ResponseVO update(Keyword keyword) {
		keywordService.updateKeyword(keyword);
		ResponseVO response = new ResponseVO();
		response.setStatusCode(StatusCode.SUCCESS);
		response.setMessage("更新成功!");
		response.setNavTabId("keyword_list");
		response.setCallbackType("closeCurrent");
		return response;
	}

}
